package de.listrong.da.main;

import gate.AnnotationSet;
import gate.Corpus;
import gate.Document;

import de.listrong.da.ontology.FileSource;
import de.listrong.da.ontology.SentenceList;
import de.listrong.da.ontology.util.AboxWriter;
import de.listrong.da.tools.IdGenerator;
import de.listrong.da.metainfo.datamodel.MetaInfo;

import java.util.HashMap;
import java.io.File;

import org.apache.log4j.Logger;

public class OntologyProcess {
    public static Logger myLogger = Logger.getLogger(OntologyProcess.class);

    public static String DEFAULT_OUTPUT_ORDER = "e:\\ontologies\\aboxes";

    public static void aboxGenerator(Corpus docCorpus, HashMap<String, MetaInfo> metaInfoList) {
        myLogger.info("Generate ABoxes in Folder " + DEFAULT_OUTPUT_ORDER);

        for (int i = 0; i < docCorpus.size(); i++) {
            
            // create an a-box
            AboxWriter aboxWriter = new AboxWriter();

            // the id for instance begins with 0 by each new document.
            IdGenerator.getInstance().clear();

            Document doc = (Document) docCorpus.get(i);            // get document object
            String key = new File(doc.getSourceUrl().toString()).getName();
            MetaInfo metainfo = metaInfoList.get(key);
            FileSource fSource = new FileSource(doc, metainfo); // create FileSource object for a document object
            aboxWriter.writeDocumentInstance(fSource);            // create an Individual in A-Box

            // System.out.println(fSource); 					// test output

            /*
            * create a list for sentences, each sentenceNote has a list for
            * instances(ClassInstance or PropertyInstance). They are in the
            * same order like the position in the sentence.
            */
            AnnotationSet sentenceAS = doc.getAnnotations("Sentences");    // load the Sentences Annotation Set
            SentenceList sentenceList = new SentenceList(sentenceAS);    // put the element from set into the list, order by position
            fSource.setSenteces(sentenceList);                            // set the sentencesList property for FileSource object

            // System.out.println(fSource.getSenteces());

            AnnotationSet instancesAS = doc.getAnnotations("Instances");    // load the Instances Annotation Set
            fSource.getSenteces().fillWithAnnotationSet(instancesAS);        // put the Instance elements into the WordList for a sentence
            /*
           for (SentenceIndividual sentence : sentenceList) {
               System.out.println(sentence);
           }
           */

            aboxWriter.writeSenttenceInstance( sentenceList, String.valueOf(fSource.getId()) );
            aboxWriter.writeToFile(DEFAULT_OUTPUT_ORDER);

        }
    }

}
